<?php
/** 
 * Installation file
 *
 * @author RolandD Cyber Produksi
 * @link http://www.csvimproved.com
 * @copyright Copyright (C) 2006 - 2008 RolandD Cyber Produksi
 * @version $Id: productexport.php 587 2008-12-02 21:39:07Z Suami $
 *
 */

/* ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );

function com_install(){
	$db = JFactory::getDBO();
	
	/* Check if there is a csvi_configuration table */
	$q = "SHOW TABLES LIKE ".$db->Quote('%csvi_configuration');
	$db->setQuery($q);
	$table_exists = $db->loadResult();
	
	if ($table_exists) {
		/* Rename templates table */
		$q = "RENAME TABLE ".$db->nameQuote('#__csvi_configuration')." TO ".$db->nameQuote('#__csvi_template_fields');
		$db->setQuery($q);
		if ($db->query()) echo 'Table csvi_configuration successfully renamed to csvi_template_fields';
		else echo 'There was a problem renaming csvi_configuration to csvi_template_fields. The error is: '.$db->getErrorMsg();
		echo '<br />';
	}
	
	/* Rename template types */
	$q = "UPDATE ".$db->nameQuote('#__csvi_templates')." SET template_type = REPLACE(template_type, 'Upload', 'Import');";
	$db->setQuery($q);
	if ($db->query()) echo 'Template types have been updated.';
	else echo 'There was a problem updating the template types. The error is: '.$db->getErrorMsg();
	echo '<br />';
	
	/* Add missing fields csvi_templates */
	/* Get the current columns */
	$q = "SHOW COLUMNS FROM ".$db->nameQuote('#__csvi_templates');
	$db->setQuery($q);
	$cols = $db->loadObjectList('Field');
	
	/* Check if we have the ID column */
	if (array_key_exists('ID', $cols)) {
		/* Rename the ID field */
		$q = "ALTER TABLE ".$db->nameQuote('#__csvi_templates')." CHANGE ".$db->nameQuote('ID')." ".$db->nameQuote('template_id')." TINYINT( 2 ) NOT NULL AUTO_INCREMENT";
		$db->setQuery($q);
		if ($db->query()) echo 'Templates ID field has been renamed to template_id.';
		else echo 'There was a problem renaming ID field to template_id. The error is: '.$db->getErrorMsg();
		echo '<br />';
	}
	
	/* Check if we have the thumb_create column */
	if (!array_key_exists('thumb_create', $cols)) {
		$q = "ALTER IGNORE TABLE ".$db->nameQuote('#__csvi_templates')." ADD COLUMN ".$db->nameQuote('thumb_create')." TINYINT(1) NOT NULL";
		$db->setQuery($q);
		if ($db->query()) echo 'thumb_create field has been created.';
		else echo 'There was a problem creating the thumb_create field. The error is: '.$db->getErrorMsg();
		echo '<br />';
	}
	
	/* Check if we have the thumb_extension column */
	if (!array_key_exists('thumb_extension', $cols)) {
		$q = "ALTER IGNORE TABLE ".$db->nameQuote('#__csvi_templates')." ADD COLUMN ".$db->nameQuote('thumb_extension')." ENUM('none','jpg','gif','png') NOT NULL default 'none'";
		$db->setQuery($q);
		if ($db->query()) echo 'thumb_extension field has been created.';
		else echo 'There was a problem creating the thumb_extension field. The error is: '.$db->getErrorMsg();
		echo '<br />';
	}
	
	/* Check if we have the ignore_non_exist column */
	if (!array_key_exists('ignore_non_exist', $cols)) {
		$q = "ALTER IGNORE TABLE ".$db->nameQuote('#__csvi_templates')." ADD COLUMN ".$db->nameQuote('ignore_non_exist')." TINYINT(1) NOT NULL";
		$db->setQuery($q);
		if ($db->query()) echo 'ignore_non_exist field has been created.';
		else echo 'There was a problem creating the ignore_non_exist field. The error is: '.$db->getErrorMsg();
		echo '<br />';
	}
	echo '<br />';
	echo '<br />';
	echo 'This component can now be un-installed as it has no other value.';
}
?>